package sy.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.hibernate.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import sy.model.IotTalkBackURI;

import sy.service.IotTalkBackURIServiceI;
import sy.util.MessageObject;

@Controller
@RequestMapping("iotTalkBackURIController")
public class IotTalkBackURIController {
	@Autowired
	IotTalkBackURIServiceI iotTalkBackURIService;
	
	/************************** view controller ***************************/

	/**
	 * 
		 * @Title      : objectView
		 * @Description: 首页视图
		 * @param      : model
		 * @return     : String
		 * @author     : jiayiwu
		 * @time       : 2017年8月30日 
		 * @throws
	 */
	@RequestMapping(value="/view")
	public String objectView(Model model){
		List<IotTalkBackURI> talkBackURIList = iotTalkBackURIService.list();
		
		model.addAttribute("talkBackURIList",talkBackURIList);
		return "iotInterface/talkBackURI/talkBackURI";
	}
	
	/**
	 * 
		 * @Title      : newObjectInfo
		 * @Description:  TODO
		 * @param      :@param model
		 * @param      :@return
		 * @return     : String
		 * @author     : jiayiwu
		 * @time       : 2017年8月30日 下午6:05:21
		 * @throws
	 */
	@RequestMapping(value="/view/new",method=RequestMethod.GET)
	public String newObjectInfo(Model model){
		
		IotTalkBackURI talkBackURI = new IotTalkBackURI();

		model.addAttribute("talkBackURI",talkBackURI);
		model.addAttribute("typeCode",1);
		
		return "iotInterface/talkBackURI/talkBackURIFrom";
	}

	/**
	 * 
		 * @Title      : editObjectInfo
		 * @Description:  TODO
		 * @param      :@param id
		 * @param      :@param model
		 * @param      :@return
		 * @return     : String
		 * @author     : jiayiwu
		 * @time       : 2017年8月30日 下午6:06:20
		 * @throws
	 */
	@RequestMapping(value="/view/edit",params="id",method=RequestMethod.GET)
	public String editObjectInfo(@RequestParam int id,Model model){
		
		IotTalkBackURI talkBackURI = iotTalkBackURIService.getIotTalkBackURIById(id);

		model.addAttribute("talkBackURI",talkBackURI);
		model.addAttribute("typeCode",2);
		
		return "iotInterface/talkBackURI/talkBackURIFrom";
	}
	
	/************************** data controller ***************************/
	/**
	 * 
	 * @Title: getIotEntranceURI
	 * @Description: TODO
	 * @param: @return     
	 * @return: List<IotEntranceURI>     
	 * @author:  xieyuanqiu
	 * @date: 2017年8月30日 下午5:16:20
	 * @throws
	 */
	@ResponseBody
	@RequestMapping(value = "/getAllData", method = RequestMethod.POST)
	private List<IotTalkBackURI> getIotTalkBackURI() {
		List<IotTalkBackURI> list = iotTalkBackURIService.list();
		return list;
	}
	/**
	 * 
	 * @Title: getIotTalkBackURIByKey
	 * @Description: TODO
	 * @param: @param key
	 * @param: @return     
	 * @return: List<IotTalkBackURI>     
	 * @author:  xieyuanqiu
	 * @date: 2017年8月30日 下午5:16:24
	 * @throws
	 */
	@ResponseBody
	@RequestMapping(value = "/getIotTalkBackURIByKey", method = RequestMethod.POST)
	private List<IotTalkBackURI> getIotTalkBackURIByKey(@RequestParam String key) {
		List<IotTalkBackURI> list = iotTalkBackURIService.listIotTalkBackURIByKey(key);
		return list;
	}
	/**
	 * 
	 * @Title: getIotTalkBackURIById
	 * @Description: TODO
	 * @param: @param id
	 * @param: @return     
	 * @return: IotTalkBackURI     
	 * @author:  xieyuanqiu
	 * @date: 2017年8月30日 下午5:16:29
	 * @throws
	 */
	@ResponseBody
	@RequestMapping(value = "/getIotTalkBackURIById", method = RequestMethod.POST)
	private IotTalkBackURI getIotTalkBackURIById(@RequestParam Integer id) {
		IotTalkBackURI iotTalkBackURI = iotTalkBackURIService.getIotTalkBackURIById(id);
		return iotTalkBackURI;
	}
	/**
	 * 
	 * @Title: newObjectInfo
	 * @Description: TODO
	 * @param: @param iotTalkBackURI
	 * @param: @return     
	 * @return: MessageObject     
	 * @author:  xieyuanqiu
	 * @date: 2017年8月30日 下午5:16:33
	 * @throws
	 */
	@ResponseBody
	@RequestMapping(value = "/new", method = RequestMethod.POST)
	private MessageObject newObjectInfo(@RequestBody IotTalkBackURI iotTalkBackURI) {
		MessageObject messageObject = new MessageObject();
		try {
			iotTalkBackURIService.save(iotTalkBackURI);
			messageObject.setMessage("添加成功 !");
			messageObject.setStatus(true);
		} catch (Exception e) {
			e.printStackTrace();
			messageObject.setMessage("添加失败 !");
			messageObject.setStatus(false);
		}
		
		return messageObject;
	}
	/**
	 * 
	 * @Title: editObjectInfo
	 * @Description: TODO
	 * @param: @param iotTalkBackURI
	 * @param: @return     
	 * @return: MessageObject     
	 * @author:  xieyuanqiu
	 * @date: 2017年8月30日 下午5:16:40
	 * @throws
	 */
	@ResponseBody
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	private MessageObject editObjectInfo( @RequestBody IotTalkBackURI iotTalkBackURI) {
		MessageObject messageObject = new MessageObject();
		try {
			iotTalkBackURIService.update(iotTalkBackURI);
			messageObject.setMessage("编辑成功 !");
			messageObject.setStatus(true);
		} catch (Exception e) {
			e.printStackTrace();
			messageObject.setMessage("编辑失败 !");
			messageObject.setStatus(false);
		}
		
		return messageObject;
	}
	/**
	 * 
	 * @Title: delObjectInfo
	 * @Description: TODO
	 * @param: @param ids
	 * @param: @return     
	 * @return: MessageObject     
	 * @author:  xieyuanqiu
	 * @date: 2017年8月30日 下午5:16:44
	 * @throws
	 */
	@RequestMapping(value="/del", method=RequestMethod.POST)
	@ResponseBody
	private MessageObject delObjectInfo(@RequestParam("id") String ids) {
		
		MessageObject messageObject = new MessageObject();
		List<Integer> idArray = new ArrayList<Integer>();
		String[] strs = ids.split(",");
		
		for(String str : strs){
			idArray.add(Integer.parseInt(str));
		}
		try {
			iotTalkBackURIService.delete(idArray);	
			messageObject.setMessage("删除成功!");
			messageObject.setStatus(true);
		} catch (Exception e) {
			// TODO: handle exception
			messageObject.setMessage("删除失败!");
			messageObject.setStatus(false);
		}
		return messageObject;
	}
	
}
